package com.ce.webframework.sec.dao.impl;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.ce.webframework.sec.dao.ISecShortcutDao;
import com.ce.webframework.sec.model.SecShortcutDO;
import com.ce.webframework.sec.model.SecShortcutTempDO;
import com.ce.webframework.sec.model.SecToolbarTempDO;
import com.sitechasia.webx.core.dao.hibernate3.HibernateDomainObjectDao;

public class SecShortcutDaoImpl extends HibernateDomainObjectDao<SecShortcutTempDO> implements ISecShortcutDao{




	/**
	 *
	 * 功能描述:查询当前用户所有已应用的快捷方式(根据应orderNum字段排序)
	 * @param 当前用户ID
	 * @param 快捷方式类型 (FAST_JUMP==>快捷跳转 FAST_QUERY==>快捷查询)
	 * @param 企业ID
	 * @param 权限功能集合
	 *@return  List
	 */
	@SuppressWarnings("unchecked")
	public List<SecShortcutDO> getUseCutModes(Long userId,String type,Long epId,List <String>functionIds){
		if(functionIds==null) return new ArrayList<SecShortcutDO>();
		String funcIdsSplit = "";

		for(int i=0;i<functionIds.size();i++){
			System.out.println("functionIds=========="+i+" : "+functionIds.get(i).toString());
			String funcId = "'"+functionIds.get(i).toString()+"'";
			funcIdsSplit+=funcId;
			if(i!=functionIds.size()-1)
				funcIdsSplit+=",";
		}

		String hsql = "FROM SecShortcutDO cut LEFT JOIN FETCH cut.secShortcutTemp temp " +
					  "WHERE temp.functionEntityId in ("+funcIdsSplit+") AND temp.type='"+type+"' AND cut.userId='"+userId+"' AND cut.epId='"+epId+"' ORDER BY cut.orderNum ASC";
		System.out.println("hsql===============>"+hsql);
		List<SecShortcutDO> menuList = getHibernateTemplate().find(hsql);
		System.out.println("menuList=======>"+menuList.size());
		return menuList;

	}



	/**
	 *
	 * 功能描述:查询当前用户所有未应用的快捷方式(根据应创建时间字段排序)
	 * @param functionIds 功能编码
	 *
	 *@return List
	 */
	@SuppressWarnings("unchecked")
	public List<SecShortcutTempDO> getUnuseCutModes(List functionIds){
		//加一参数userId:
		String userId="";
		if(functionIds==null) return new ArrayList<SecShortcutTempDO>();
		String funcIdsSplit = "";

		for(int i=0;i<functionIds.size();i++){
			String funcId = "'"+functionIds.get(i).toString()+"'";
			funcIdsSplit+=funcId;
			if(i!=functionIds.size()-1)
				funcIdsSplit+=",";
		}

		System.out.println("funcIdsSplit=================="+funcIdsSplit);

		String hsql ="FROM SecShortcutTempDO temp where temp.functionEntityId in ("+funcIdsSplit+") " +
				"AND temp.shortcutTempId not in (" +
				"SELECT cut.shortcutTempId FROM SecShortcutDO cut WHERE cut.userId='"+userId+"')";
		List<SecShortcutTempDO> tempList = getHibernateTemplate().find(hsql);
		return tempList;
	}


	/**
	 *
	 * 功能描述:获得当前用户所有的快捷方式
	 *@param type  快捷方式类型
	 *@param functionIds 功能ID
	 *@param epId 企业ID
	 *@return
	 *
	 */
	public List<SecShortcutTempDO> getAllCutModes(String type,List functionIds,Long epId){
		if(functionIds==null) return new ArrayList<SecShortcutTempDO>();
		String funcIdsSplit = "";

		for(int i=0;i<functionIds.size();i++){
			String funcId = "'"+functionIds.get(i).toString()+"'";
			funcIdsSplit+=funcId;
			if(i!=functionIds.size()-1)
				funcIdsSplit+=",";
		}


		System.out.println("funcIdsSplit=================="+funcIdsSplit);

		String hsql ="FROM SecShortcutTempDO temp where temp.functionEntityId in ("+funcIdsSplit+") AND temp.type='"+type+"' " +
				"ORDER BY temp.createDate ASC";

		List<SecShortcutTempDO> tempList = getHibernateTemplate().find(hsql);
		return tempList;
	}


	/**
	 *
	 * 功能描述: 当前用选择或取消快捷菜单的快捷方式时增加或删除当前菜单的快捷方式
	 *
	 *@param optionFlag 操作标志(add:增加 del：删除)
	 *@param shortcutId 快捷方式主键
	 *@param userId  用户ID
	 *@param epId 企业ID
	 *
	 */
	public void updateShortcut(String optionFlag, Long shortcutId, Long userId,Long epId){
		Session session = getSession();
		System.out.println("shortcutId============="+shortcutId);
		SecShortcutTempDO temp = (SecShortcutTempDO)this.getHibernateTemplate().get(SecShortcutTempDO.class,shortcutId);//得到要查找的拖拉源对象
		System.out.println("temp==========:"+temp);
		if(temp==null) return ;
		if (optionFlag.equals("del")){
			//删除当前拖拉的对象(主要是删除其对应的用户操作相关的表)，将其回到未应用状态
			String updateNewDtsSql = "DELETE SecShortcutDO cut" +
									 " WHERE cut.secShortcutTemp.shortcutTempId ='" + temp.getShortcutTempId() +"' " +
									 "AND cut.userId='"+userId+"'";
			Query query = session.createQuery(updateNewDtsSql);
			query.executeUpdate();
			updateNewDtsSql = null;


		}
		else if(optionFlag.equals("add")){
			if(findShortcutByTempId(shortcutId,userId)==0){
				SecShortcutDO shortcut = new SecShortcutDO();
				shortcut.setCreateDate(new Date());
				shortcut.setEpId(epId);
				shortcut.setOrderNum(0);
				shortcut.setSecShortcutTemp(temp);
				shortcut.setUserId(userId);
				session.saveOrUpdate(shortcut);
			}


		}else{

			return ;

		}





	}

	/**
	 *
	 * 功能描述:查询当前用户是否有已应用的快捷方式
	 *
	 *@param tempId
	 *@param userId
	 *@return int
	 *
	 */
	public int findShortcutByTempId(Long tempId,Long userId){
		String hsql = "SELECT COUNT(*) FROM SecShortcutDO cut where cut.secShortcutTemp.shortcutTempId='"+tempId+"' " +
		"AND cut.userId='"+userId+"'";
		return new Integer(getHibernateTemplate().find(hsql).get(0).toString()).intValue();


	}


}
